#coding=UTF-8
'''
Created on 2015年3月25日

@author: Administrator

公共主页模块
'''
from django.http.response import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.db.models import Q

from lib.utils import login
from youaiwang.models import FeedbackModel
from webapp_config import homeUrl
from account.views import loginbyemail
from forum.models import Sharing as SharingModel
from account.models import User as UserDb, Message as MessageDb


def setObj(request,obj,nav_ul_dict,message_count,type):
    obj['nav_ul']=nav_ul_dict
    obj['session']=request.session
    pageindex=request.session.get("pageIndex",1)
    articleList=[]
    sharingList=[]
    questionList=[]
    module=''
    if type=='旅行足迹':
        module="travelfootmark"
        articleList=SharingModel.objects.filter(status='True',article_module='旅行足迹').order_by('createTime')
    elif type=='礼物':
        module="present"
        articleList=SharingModel.objects.filter(status='True',article_module='礼物').order_by('createTime')
    elif type=='心声':
        module="thinking"
        articleList=SharingModel.objects.filter(status='True',article_module='心声').order_by('createTime')
    elif type=="幸福墙":
        module="happywall"
    elif type=="消息":
        module="message"
    if articleList:
        sharingList=articleList.filter(article_type='share')
        questionList=articleList.filter(article_type='question')
    #切片右边不到达
    obj['shareList']=sharingList[((pageindex-1)*5):(5*pageindex)]
    obj['questionList']=questionList[0:10]
    obj['pagecount']=range(1,(len(sharingList)/5+2))
    obj['message_count']=updateNewMessage(request,obj)
    obj['module']=module 
    

def setUserObj():
    pass

def updateNewMessage(request,obj):
    #已经登录
    try:
        userId = request.session.get('userId','')
        user = UserDb.objects.get(id=userId, status=1)
        msgList = MessageDb.objects.filter(Q(originUser=user)|Q(toPer=user)).exclude(fromPer=user).order_by('-createTime')
        obj['msgList']=msgList
        return msgList.filter(status=1).count()
    #未登录
    except:
        pass


    
    

def travelfootmark(request,pageIndex):
    obj={}
    if request.method=="GET":
        request.session['pageIndex']=int(pageIndex)
        nav_ul_dict={'first':" active"}
        message_count=1
        type='旅行足迹'
        setObj(request,obj,nav_ul_dict,message_count,type)
        
        return render_to_response('home/index.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        if request.method=="POST":
            return loginbyemail(request)
        return HttpResponse('Neither GET NOR POST!')
            
    
def present(request,pageIndex):
    obj={}
    if request.method=="GET":
        request.session['pageIndex']=int(pageIndex)
        nav_ul_dict={'second':" active"}
        message_count=1
        type='礼物'
        setObj(request,obj,nav_ul_dict,message_count,type)
        return render_to_response('home/present.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        if request.method=="POST":
            return loginbyemail(request)
        return HttpResponse('Neither GET NOR POST!') 
    
def thinking(request,pageIndex):
    obj={}
    if request.method=="GET":
        request.session['pageIndex']=int(pageIndex)
        nav_ul_dict={'third':" active"}
        message_count=3
        type='心声'
        setObj(request,obj,nav_ul_dict,message_count,type)
        return render_to_response('home/thinking.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        if request.method=="POST":
            return loginbyemail(request)
        return HttpResponse('Neither GET NOR POST!') 
    
def happywall(request):
    obj={'count':range(0,15)}
    if request.method=="GET":
        
        nav_ul_dict={'fourth':" active"}
        message_count=4
        type='幸福墙'
        setObj(request,obj,nav_ul_dict,message_count,type)
        return render_to_response('home/happywall.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        if request.method=="POST":
            return loginbyemail(request)
        return HttpResponse('Neither GET NOR POST!') 
    
# def message(request):
#     obj={'count':range(0,15)}
#     if request.method=="GET":
#         nav_ul_dict={'fifth':" active"}
#         message_count=0
#         type = '消息'
#         setObj(request,obj,nav_ul_dict,message_count,type)
#         return render_to_response('home/message.html',{'obj':obj},context_instance=RequestContext(request))
#     else:
#         if request.method=="POST":
#             return loginbyemail(request)
#         return HttpResponse('Neither GET NOR POST!')


@login
def feedback(request):
    obj={}
    if request.method=="GET":
        return render_to_response('home/feedback.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        content = request.POST.get("feedback_editor")
        FeedbackModel.objects.create(content=content)
        return HttpResponseRedirect(homeUrl)

#-----------------------------------------------------v2 code-----------------------------------------------------------

def index(request):
    return HttpResponseRedirect('/home/1/')

def home_with_page(request, pageIndex):
    obj={}
    if request.method=="GET":
        obj['session']=request.session
        module="travelfootmark"
        request.session['pageIndex'] = pageIndex #todo， 为了让前端知道这是第几页，也可以用js实现
        pageIndex = int(pageIndex)
        sharingList=SharingModel.objects.filter(status=1).order_by('-createTime')
        #切片右边不到达
        obj['shareList']=sharingList[((pageIndex-1)*5):(5*pageIndex)]
        obj['pagecount']=range(1,(len(sharingList)/5+2))
        obj['message_count']=updateNewMessage(request,obj)
        obj['module']=module

        return render_to_response('home/index.html',{'obj':obj},context_instance=RequestContext(request))
    else:
        if request.method=="POST":
            return loginbyemail(request)
        return HttpResponse('Neither GET NOR POST!')



@login
def sharing(request, pageIndex):

    if request.method=="GET":
        request.session['pageIndex'] = pageIndex #todo， 为了让前端知道这是第几页，也可以用js实现
        pageIndex = int(pageIndex)
        sharingList=SharingModel.objects.filter(status=True).order_by('-createTime')

        obj = {
            'shareList': sharingList[((pageIndex-1)*5):(5*pageIndex)], #切片右边不到达
            'pageCount': range(1,(len(sharingList)/5+2)),
            'session': request.session,
        }

        return render_to_response('home/index.html', {'obj':obj}, context_instance=RequestContext(request))
    else:
        return HttpResponse('404 ERROR!')



#-----------------------------------------------------v2 code-----------------------------------------------------------

if __name__ == '__main__':
    pass